package com.miracle.main.module.mvp.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.laojiang.imagepickers.data.MediaDataBean;
import com.laojiang.imagepickers.ui.pager.view.ImagePagerActivity;
import com.miracle.basemvp.base.BaseFragment;
import com.miracle.basemvp.base.state.StateController;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.common.base.BaseListFragment;
import com.miracle.common.util.CommonConstants;
import com.miracle.main.R;
import com.miracle.main.R2;
import com.miracle.main.adapter.FileListAdapter;
import com.miracle.main.module.di.component.DaggerFileListComponent;
import com.miracle.main.module.di.module.FileListModule;
import com.miracle.main.module.mvp.contract.FileListContract;
import com.miracle.main.module.mvp.entity.bean.FileItem;
import com.miracle.main.module.mvp.presenter.FileListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * com.miracle.main.module.mvp.ui.fragment
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/11 9:12
 * @see com.miracle.main.module.mvp.ui.fragment
 */
public class FileListFragment extends BaseListFragment<FileListPresenter, FileItem> implements
        FileListContract
                .View ,FileListAdapter.PicClickListener{
    public static final String TYPE = "type";
    public static final int TYPE_ALL = 0;
    public static final int TYPE_PIC = 1;
    public static final int TYPE_DOC = 2;
    public static final int TYPE_NOTE = 3;

    @BindView(R2.id.rv_file_list)
    RecyclerView mRcv;
    @BindView(R2.id.srl_file_list)
    SmartRefreshLayout mSmartRefreshLayout;

    @Inject
    ArrayList<FileItem> mFileList;
    @Inject
    FileListAdapter mAdapter;

    int mType;

    public static FileListFragment mFragment;

    public static FileListFragment newInstance(int type) {
        mFragment = new FileListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(TYPE, type);
        mFragment.setArguments(bundle);
        return mFragment;
    }

    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.cloud_fragment_file_list;
    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {
        DaggerFileListComponent
                .builder()
                .appComponent(appComponent)
                .fileListModule(new FileListModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void initView() {
        GridLayoutManager manager = new GridLayoutManager(mContext, 3);
        mAdapter.setListener(this);
        mAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                if (mFileList.get(position).getItemType() == FileItem.TYPE_IMG) {
                    return 1;
                } else {
                    return 3;
                }
            }
        });
        mRcv.setLayoutManager(manager);
        mRcv.setAdapter(mAdapter);
        mSmartRefreshLayout.setOnRefreshListener(this);
        mSmartRefreshLayout.setOnLoadmoreListener(this);
    }

    @Override
    protected void initStateController() {
        super.initStateController();
        mStateController = new StateController.Builder()
                .initBuilder(this)
                .setLayoutState(R.id.ll_no_data_file_list)
                .setSmartRefreshLayout(R.id.srl_file_list)
                .setLoadingLayout(R.id.frame_progress)
                .setStateImageView(R.id.iv_image)
                .build();
    }

    @Override
    public void initData() {
        mType = getArguments().getInt(TYPE);
    }

    @Override
    public List<FileItem> setList() {
        return mFileList;
    }

    @Override
    public RecyclerView.Adapter setAdapter() {
        return mAdapter;
    }

    @Override
    public void requestData() {
        mPresenter.getFileList(providerEvent(), mType, mStateController);
    }

    @Override
    protected List<FileItem> filter(List<FileItem> list) {
        for (int i = 0; i < list.size(); i++) {
            String path = list.get(i).getPath();
            if (!TextUtils.isEmpty(path)) {
                path = path.toLowerCase();
                if (path.contains("jpg") || path.contains("png") || path.contains("jpeg")) {
                    list.get(i).setmItemType(FileItem.TYPE_IMG);
                }
            }
        }
        return list;
    }

    @Override
    public void onClick(int i) {
        ImagePagerActivity.start(mActivity, getMediaList(), i);
    }

    private ArrayList<MediaDataBean> getMediaList() {
        ArrayList<MediaDataBean> list = new ArrayList<>();
        for (int i = 0; i < mFileList.size(); i++) {
            MediaDataBean bean = new MediaDataBean();
            bean.setMediaPath(mFileList.get(i).getPath());
            list.add(bean);
        }
        return list;
    }

}
